package com.game.play;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.game.cards.BaseCard;
import com.game.cards.MonsterCard;
import com.game.cards.TrapCard;

public class ActionQueue {
	private List<String> events;
	private List<TrapCard> actions;
	private List<MonsterCard> effects;
	private Game game;
	
	public ActionQueue()
	{
		this.events = new ArrayList<String>();
		this.actions = new ArrayList<TrapCard>();
		this.effects = new ArrayList<MonsterCard>();
		this.game = null;
	}
	public void addEvent(String event)
	{
		this.events.add(event);
		this.checkEvents(event);
	}
	public void addTrap(TrapCard tc)
	{
		this.actions.add(tc);
	}
	public void addEffect(MonsterCard mc)
	{
		this.effects.add(mc);
	}
	public void checkEvents(String event)
	{
		String[] div = event.split(",");
		//TODO add more support
		Events e = Events.valueOf(div[0]);
		
		for (TrapCard tc : this.actions)
		{
			if(tc.getTrigger() == e) {
				if (tc.test(game)) {
					this.actions.remove(tc);
					break;
				}
			}
		}
		for (MonsterCard mc : this.effects)
		{
			if(mc.getTrigger() == e) {
				mc.test(game);
			}
		}
	}
	public String[] getEventArgs(Events evt)
	{
		String[] div = this.getMostRecentEvent().split(",");
		return div;
	}
	public String getMostRecentEvent()
	{
		List<String> temp = new ArrayList<String>();
		temp.addAll(this.events);
		Collections.reverse(temp);
		
		return temp.get(0);
	}
	public String getMostRecentEvent(Events evt)
	{
		List<String> temp = new ArrayList<String>();
		 temp.addAll(this.events);
		Collections.reverse(temp);
		
		for (String s : temp)
		{
			if (evt.toString() == s.split(",")[0]) return s;
		}
		return null;
	}
	public List<String> getMostRecentEvents(int amount)
	{
		List<String> temp = new ArrayList<String>();
		 temp.addAll(this.events);
		if (amount > temp.size()) amount = temp.size();
		
		Collections.reverse(temp);
		
		
		return temp.subList(0, 4);
	}
	
	public Game getGame() {
		return game;
	}
	public void setGame(Game game) {
		this.game = game;
	}
	public List<TrapCard> getActions() {
		return actions;
	}
	public void setActions(List<TrapCard> actions) {
		this.actions = actions;
	}
	public void printActions()
	{
		List<String> temp = new ArrayList<String>();
		 temp.addAll(this.events);
		Collections.reverse(temp);
		for (String s : temp)
		{
			System.out.println(s);
		}
		Collections.reverse(temp);
	}
	public void printMostRecentAction()
	{
		System.out.println(this.getMostRecentEvent());
	}
	public void printMostRecentActions(int amount)
	{
		for (String s : this.getMostRecentEvents(amount))
		{
			System.out.println(s);
		}
	}
	public List<String> getEvents() {
		return events;
	}
	public void setEvents(List<String> events) {
		this.events = events;
	}
	public List<MonsterCard> getEffects() {
		return effects;
	}
	public void setEffects(List<MonsterCard> effects) {
		this.effects = effects;
	}
	
}
